package com.android.dfsc;

import com.android.dfsc.data.HrisCommObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SettingActivity extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                SharedPreferences   clientConfig = getSharedPreferences("client_config",0);

                LayoutInflater factory = LayoutInflater.from(this);
                final View settingView = factory.inflate(R.layout.setting, null);
                this.setContentView(settingView);
                
                EditText ipSetting = (EditText) settingView
				.findViewById(R.id.IPSetting);
                ipSetting.setText( clientConfig.getString("IP",""));
                
                ((EditText) settingView
				.findViewById(R.id.UserNameSetting)).setText(clientConfig.getString("user_name",""));
                
                ((EditText) settingView
        				.findViewById(R.id.PasswordSetting)).setText(clientConfig.getString("password",""));
                
                ((EditText) settingView
        				.findViewById(R.id.AuthCodeSetting)).setText(clientConfig.getString("auth_code",""));
                ((TextView) settingView
				.findViewById(R.id.AuthString)).setText(SmartHomeEngine.getInstance().getAuthCode());
            
                Button buttonSave = (Button) settingView
					.findViewById(R.id.button_save);
                Button buttonCancel = (Button) settingView
					.findViewById(R.id.button_cancel);
                buttonSave.setOnClickListener(new Button.OnClickListener() {
                	public void onClick(View v) {
                		SharedPreferences   clientConfig =  getSharedPreferences("client_config",0);
                		Editor clientConfigEditor = clientConfig.edit();
                		EditText ipSetting = (EditText) settingView
        				.findViewById(R.id.IPSetting);
                		
                		clientConfigEditor.putString("IP",ipSetting.getText().toString() );
                		clientConfigEditor.putString("user_name",((EditText) settingView
                				.findViewById(R.id.UserNameSetting)).getText().toString() );
                		clientConfigEditor.putString("password",((EditText) settingView
                				.findViewById(R.id.PasswordSetting)).getText().toString() );
                		clientConfigEditor.putString("auth_code",((EditText) settingView
                				.findViewById(R.id.AuthCodeSetting)).getText().toString() );
                		//clientConfigEditor.putString("auth_code","89ab4c5599635b0a" );
                		
                		clientConfigEditor.commit();
                		SmartHomeEngine.getInstance().setConfig();
                		finish();
                	}
                });
                buttonCancel.setOnClickListener(new Button.OnClickListener() {
                	public void onClick(View v) {
                		finish();
                	}
                });
//                AlertDialog dlg = new AlertDialog.Builder(this)
//                .setTitle(getString(R.string.setting))
//                .setView(settingView)
//                .setPositiveButton(getString(R.string.setting_ok), new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//                    	
//                    	//TODO
//                    	finish();
//                    	
//                        /* User clicked OK so do some stuff */
//                    }
//                })
//                .setNegativeButton(getString(R.string.setting_cancel), new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//                    	 finish(); /* User clicked cancel so do some stuff */
//                    }
//                }).create();
//               dlg.show();
               
                     
    }

}
